<?php namespace backend\controllers;

use yii\filters\AccessControl;
use Yii;

class BaseController extends \yii\web\Controller
{
    public $layout = 'index';

    public function behaviors ()
    {
        return [
            'access' => [
                'class' => AccessControl::className() ,
                'rules' => [
                    [
                        'actions' => [
                            'login' ,
                            'error' ,
                        ] ,
                        'allow'   => TRUE ,
                        'roles'   => [ '?' ] ,
                    ] ,
                    [
                        'allow' => TRUE ,
                        'roles' => [ '@' ] ,
                    ] ,
                ] ,
            ] ,
        ];
    }
    /**
     * @return array
     *
     * 上传轮播图
     */

    public function actionBannerImg ()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $request = Yii::$app->request;

        if ( $request->isPost ) {

            //文件上传存放的目录
            $dir = IMG_ROOT .'/banner-img/'. date( "Ymd" );
            $res = $this->upload( $dir ,'banner-img');
            return $res;
        } else {
            return [ 'code' => 1 , 'error' => 'no data' ];
        }

    }

    /**
     * @return array
     *
     * 上传导入文件
     */
    public function actionFile ()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $request = Yii::$app->request;

        if ( $request->isPost ) {

            //文件上传存放的目录
            $dir = IMG_ROOT . "/file/" . date( "Ymd" );

            $res = $this->upload( $dir , 'file' );

            return $res;
        } else {
            return [ 'code' => 1 , 'error' => 'no data' ];
        }

    }

    /**
     * @param        $url
     * @param string $msg
     *
     * @return \yii\web\Response
     *
     * 封装操作成功提示并跳转方法
     */
    public function success ( $url , $msg = '操作成功' )
    {
        \Yii::$app->getSession()->setFlash( 'success' , $msg , FALSE );

        return $this->redirect( $url );
    }

    /**
     * @param        $url
     * @param string $msg
     *
     * @return \yii\web\Response
     *
     * 封装操作失败提示并跳转方法
     */
    public function error ( $url , $msg = '操作失败' )
    {
        \Yii::$app->getSession()->setFlash( 'error' , $msg , FALSE );

        return $this->redirect( $url );
    }

    /**
     * @param $dir
     * @param $folderName
     *
     * @return array
     *
     * 上传方法
     */
    private function upload ( $dir , $folderName )
    {
        //文件上传存放的目录
        is_dir( $dir ) || mkdir( $dir , 0777 , TRUE );

        //文件名
        $fileName = date( "HiiHsHis" ) . mt_rand( 0 , 9999 ) . "." . pathinfo( $_FILES[ 'file' ][ 'name' ] )[ 'extension' ];

        $dir = $dir . '/' . $fileName;

        @move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ] , $dir );

        $uploadSuccessPath = '/upload/'. $folderName . '/' . date( "Ymd" ) . '/' . $fileName;

        return [
            'code'       => 0 ,
            'url'        => IMG_URL . $uploadSuccessPath ,
            'attachment' => $uploadSuccessPath ,
        ];
    }
}